﻿<shared:UserControlBase x:Class="HP.Cachalote.WPF.UserControls.TodoLabelTreeViewUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:userControls="clr-namespace:HP.Cachalote.WPF.UserControls"
             xmlns:shared="clr-namespace:HP.Cachalote.WPF.Shared"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" 
             DataContext="{Binding LabelTreeView, Source={StaticResource Locator}}">
    <UserControl.Resources>
        <ContextMenu x:Key="ContextMenuTodoLabelItem">
            <MenuItem Header="Edit" Command="{Binding EditCommand}" />
            <Separator />
            <MenuItem Header="Add label above" Command="{Binding AddLabelAboveCommand}" />
            <MenuItem Header="Add label below" Command="{Binding AddLabelBelowCommand}" />
            <MenuItem Header="Add child label" Command="{Binding AddChildLabelCommand}" />
            <MenuItem Header="Delete" Command="{Binding DeleteLabelCommand}" />
        </ContextMenu>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Border Style="{StaticResource ListTitleBorder}">
            <TextBlock Text="Categories" Style="{StaticResource ListTitleTextBlock}" />
        </Border>
        <TreeView Grid.Row="1" x:Name="TreeViewLabels" ItemsSource="{Binding LabelList}" BorderThickness="0">
            <TreeView.Resources>
                <SolidColorBrush Color="#d0e5fe" x:Key="{x:Static SystemColors.HighlightBrushKey}" />
                <SolidColorBrush Color="Black" x:Key="{x:Static SystemColors.HighlightTextBrushKey}" />
            </TreeView.Resources>
            <!-- Set ItemContainerStyle for binding IsExpanded property. It can help me to load nodes by demand. -->
            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <!--<Style.Triggers>
                        <Trigger Property="IsFocused" Value="False">
                            <Setter Property="IsSelected" Value="False" />
                        </Trigger>
                    </Style.Triggers>-->
                    <EventSetter Event="LostFocus" Handler="TreeViewLabelsItem_LostFocus" />
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                    <Setter Property="ContextMenu" Value="{StaticResource ContextMenuTodoLabelItem}" />
                    <EventSetter Event="TreeViewItem.PreviewMouseRightButtonDown" Handler="TreeViewLabelsItem_PreviewMouseRightButtonDown"/>
                </Style>
            </TreeView.ItemContainerStyle>
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding SubLabels}">
                    <userControls:TodoLabelTreeViewItemUserControl DataContext="{Binding}" />
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</shared:UserControlBase>
